package com.yourname.lab4.v2;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Exercise {
    private List<AbstractExpression> expressions;

    public Exercise() {
        this.expressions = new ArrayList<>();
    }

    public void addExpression(AbstractExpression exp) {
        expressions.add(exp);
    }

    public void generateAddition(int count) {
        Random rand = new Random();
        for (int i = 0; i < count; i++) {
            int left = rand.nextInt(100);
            int right = rand.nextInt(100);
            expressions.add(new AdditionExpression(left, right));
        }
    }

    public void generateSubtraction(int count) {
        Random rand = new Random();
        for (int i = 0; i < count; i++) {
            int left = rand.nextInt(100) + 1;
            int right = rand.nextInt(left);
            expressions.add(new SubtractionExpression(left, right));
        }
    }

    public void generateMixed(int count) {
        Random rand = new Random();
        for (int i = 0; i < count; i++) {
            if (rand.nextBoolean()) {
                // 生成加法
                int left = rand.nextInt(100);
                int right = rand.nextInt(100);
                expressions.add(new AdditionExpression(left, right));
            } else {
                // 生成减法
                int left = rand.nextInt(100) + 1;
                int right = rand.nextInt(left);
                expressions.add(new SubtractionExpression(left, right));
            }
        }
    }

    public void displayFormatted() {
        System.out.println("=== 数学习题 ===");
        for (int i = 0; i < expressions.size(); i++) {
            System.out.printf("%2d. %s\n", i + 1, expressions.get(i));
        }
        System.out.println("================");
    }

    public List<AbstractExpression> getExpressions() {
        return expressions;
    }
}